﻿/**********************************************************\
*  TinyLib - AnimatedSprite                                *
*                                                          *
*  Animates a sprite from a sprite sheet.                  *
*                                                          *
* (c) Jens Berglind                                        *
*                                                          *
* Last edited by Jens Berglind 25-06-2012                  *
\**********************************************************/ 

using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TinyLib.Sprite
{
    public class AnimatedSprite : Sprite
    {
        #region Fields

        public Dictionary<string, Strip> strips = new Dictionary<string, Strip>();
        public Vector2 frameSize;
        public string strCurrentAnimation = "";

        bool bIsAnimating = true;
        Vector2 velocity;

        #endregion

        #region Properties

        public Color Color
        {
            get { return color; }
            set { color = value; }
        }

        public bool BoolIsAnimating
        {
            get { return bIsAnimating; }
            set { bIsAnimating = value; }
        }
        /*
        public int IntWidth
        {
            get { return strips[strCurrentAnimation].IntFrameWidth; }
        }

        public int IntHeight
        {
            get { return strips[strCurrentAnimation].IntFrameHeight; }
        }
        */
        /*
        public float FloatSpeed
        {
            get { return fSpeed; }
            set { fSpeed = MathHelper.Clamp(fSpeed, 1.0f, 400.0f); }
        }
        */
        public Vector2 Position
        {
            get { return position; }
            set
            {
                position = value;
                if (velocity != Vector2.Zero)
                { velocity.Normalize(); }
            }
        }

        public string Animation
        {
            get { return strCurrentAnimation; }
            set
            {
                strips[strCurrentAnimation].Reset();
                strCurrentAnimation = value;
            }
        }

        public Vector2 FrameSize
        {
            get { return frameSize; }
            set { frameSize = value; }
        }

        #endregion

        #region Init

        public AnimatedSprite(SpriteBatch spriteBatch, Texture2D texture, Game game)
            : base(spriteBatch, texture, game)
        {
            spritebatch = spriteBatch;
        }

        #endregion

        #region Methods

        public void AddStrip(string id, int numberOfFrames, int timeOnChange)
        {
            Strip strip = new Strip();
            strip.timeOnChange = timeOnChange;
            int frameWidth = texture.Width / (int)frameSize.X;
            int frameHeight = texture.Height / (int)frameSize.Y;

            Rectangle rectangle;
            rectangle.Y = 0 + (frameHeight * strips.Count);
            rectangle.Width = frameWidth;
            rectangle.Height = frameHeight;
            for (int i = 0; i < numberOfFrames; i++)
            {
                rectangle.X = i * frameWidth;
                strip.AddRectangle(rectangle);
            }

            strips[id] = strip;
            strCurrentAnimation = id;
        }

        public override void Draw(GameTime gameTime)
        {
            spritebatch.Draw(texture, position, strips[strCurrentAnimation].GetRectangle(gameTime), color, rotation, origin, scale, effect, DrawOrder);
        }
        
        public void DrawDeath(GameTime gameTime)
        {
            int rects = strips[strCurrentAnimation].rectangles.Count;

            spritebatch.Draw(texture, position, strips[strCurrentAnimation].rectangles[(rects - 1)], color, rotation, origin, scale, effect, DrawOrder);
        }
        
        #endregion
    }
}
